package com.bhf.security;

import com.bhf.constant.ResultCodeEnum;
import com.bhf.utils.RestResult;
import com.bhf.utils.RestResultUtils;
import org.elasticsearch.ElasticsearchStatusException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.ConnectException;

/**
 * @author pwq
 */
@RestControllerAdvice
public class ExceptionControllerHandler {

    @ExceptionHandler(value = Exception.class)
    public RestResult<Object> errorHandler(Exception ex) {
        if (ex instanceof ConnectException){
            return RestResultUtils.buildResult(ResultCodeEnum.CONNECT_TIMEOUT_ERROR);
        }
        if (ex instanceof ElasticsearchStatusException){
            return RestResultUtils.buildResult(ResultCodeEnum.NOT_KNOWN_INDEX_ERROR);
        }
        return RestResultUtils.buildResult(ResultCodeEnum.ERROR);
    }
}
